package com.yz.admin.login.service.impl;

import com.yz.admin.login.dao.UserDao;
import com.yz.admin.login.pojos.User;
import com.yz.admin.login.service.UserService;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public User findUser(String phone) {
        Example example = new Example(User.class);
        example.and().andEqualTo("phone",phone);
        return userDao.selectOneByExample(example);
    }

    @Override
    public void save(User user) {
        user.setPassword(secretShiroPass(user.getPhone(),user.getPassword()));
        userDao.insertSelective(user);
    }


    private String secretShiroPass(String saltStr, String password) {
        String hashAlgorithmName = "MD5";//加密方式
        ByteSource salt = ByteSource.Util.bytes(saltStr);//以账号作为盐值

        int hashIterations = 56;// 加密56次（注意这里配置的值要与shiro的配置文件中配置的值保持一致。否则无法解密

        SimpleHash hash = new SimpleHash(hashAlgorithmName, password, salt, hashIterations);
        return hash.toString();
    }
}
